import Sprite from '../base/sprite'
import { SPCIES_TYPE } from '../../constant/index'
import { getCloudUrl } from '../../utils'

const BG_IMG_SRC = getCloudUrl('images/bg.jpg')
const FONT_SIZE =  25
const MAX_DRIFT = 50
const SPEND = 0.5
/**
 * 获得分数
 */
export default class Bonus extends Sprite {
  constructor() {
    super(BG_IMG_SRC, 30, 30)
    this.parentWidth = 0
    this.value = 0
    this.scale = 0.5
    this.drift = 0
    this.spend = SPEND
    this.darwingType = SPCIES_TYPE.GOLD.type
    this.MAX_DRIFT = MAX_DRIFT
    this.renderingScore = ''
    this.animationCurve = [
      {
        count: 0,
        spend: 0.02
      },
      {
        count: 20,
        spend: 0.02
      }
    ]
    this.opacity = 1
    this.fillStyle = `rgba(153, 42, 8, ${this.opacity})`
  }
  setSrc (src) {
    this.img.src = src
  }
  destry (parent) {
    parent.removeChild(this)
  }
  update(parent) {
    if (this.drift >= MAX_DRIFT) {
      parent.removeChild(this)
    }
    this.left = (parent.width - this.width * this.scale) / 2
    this.x = parent.x + this.left
    this.y = parent.y + this.top + 15
    this.parentWidth = parent.width
    this.top -= this.spend
    this.drift += this.spend

    const curitem = this.animationCurve.find(item => item.count >= this.renderingCount)
    if (curitem) {
      this.scale += curitem.spend
    } else {
      this.scale += 0.005
    }
    if (this.renderingCount > 10) {
      this.opacity -= 0.01
      this.fillStyle = `rgba(153, 42, 8, ${this.opacity})`
    }
    this.renderingCount += 1
    if (this.darwingType === SPCIES_TYPE.GOLD.type) {
      this.renderingScore = `+ ${this.value}`
    } else if (this.darwingType === SPCIES_TYPE.BOMB.type) {
      this.renderingScore = `- ${this.value}`
    }
  }
  render(ctx) {
    ctx.fillStyle = this.fillStyle
    ctx.font = `${FONT_SIZE * this.scale}px Arial`
    ctx.fillText(
      this.renderingScore,
      this.x,
      this.y
    )
  }
}
